<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xml:lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"><head>

  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Hibernate 与 Spring 多数据源的配置 - Spring - Java - JavaEye论坛</title>
    <meta name="description" content="Spring2.0.1以后的版本已经支持配置多数据源，并且可以在运行的时候动态加载不同的数据源。通过继承AbstractRoutingDataSource就可以实现多数据源的动态转换。目前做的项目就是需要访问12个数据源，每个数据源的表结构都是相同的，所以要求数据源的变动对于编码人员来说是透明，也就是说同样SQL语句在不同的环境下操作的数据库是不一样的。具体的配置如下： 一、首先需要写一个静态的键值 ...">
    <meta name="keywords" content=" Spring Java">
    <link rel="shortcut icon" href="http://www.javaeye.com/images/favicon.ico" type="image/x-icon">
    <link href="http://www.javaeye.com/rss/board/Java" rel="alternate" title="JavaEye论坛" type="application/rss+xml">
    <link href="78432_files/forum.css" media="screen" rel="stylesheet" type="text/css">
    <script src="78432_files/application.js" type="text/javascript"></script>
      <link href="78432_files/SyntaxHighlighter.css" media="screen" rel="stylesheet" type="text/css">
  <script src="78432_files/shCoreCommon.js" type="text/javascript"></script>
<script src="78432_files/hotkey.js" type="text/javascript"></script>
          <script src="78432_files/dragdrop.js" type="text/javascript"></script>
          <style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style><style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style><style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style><style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style><style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style><style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style><style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style></head><body>
    <div id="page">
      <div id="header">
        <div class="clearfix"><div id="user_nav">
      <a href="http://dengjie200.javaeye.com/" title="查看我的博客首页" class="welcome">欢迎dengjie200</a>
    
      <a href="http://app.javaeye.com/messages" title="你有新的站内短信"><img alt="Newpm" src="78432_files/newpm.gif">收件箱(1)</a>
    
    
      <a href="http://app.javaeye.com/" title="我的应用首页">我的应用</a>
    
    <div class="quick_menu" style="display: none;">
      <a href="http://app.javaeye.com/feed" title="我订阅的好友动态消息">我的订阅</a>
      <a href="http://app.javaeye.com/feed/my" title="我参与的话题更新的消息">我的参与</a>
      <a href="http://app.javaeye.com/chat" title="用闲聊发表简短的话题">我的闲聊</a>
      <a href="http://app.javaeye.com/mygroup" title="我加入的圈子最新话题">我的圈子</a>
      <a href="http://app.javaeye.com/links" title="我收藏的网络资源链接">我的收藏</a>
      <a href="http://app.javaeye.com/myresume" title="我的个人简历">我的简历</a>
    </div>
        <a href="http://dengjie200.javaeye.com/admin" title="管理我的博客">我的博客</a>
    <a href="http://app.javaeye.com/profile" title="修改我的个人设置">设置</a>
    <a href="http://www.javaeye.com/logout" class="nobg" onclick="var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'put'); f.appendChild(m);f.submit();return false;">退出</a>
  </div></div>
        <div class="clearfix">
          <div id="branding">
            <a href="http://www.javaeye.com/"><img alt="JavaEye-最棒的软件开发交流社区" src="78432_files/logo.gif" title="JavaEye-最棒的软件开发交流社区"></a>
          </div>
          <div id="gg"><a href="http://www.javaeye.com/clicks/226" target="_blank"><img src="78432_files/thoughtworks.jpg"></a></div>
        </div>
      </div>

      <div id="content" class="clearfix">
        <div id="main">
          


          

<div class="clearfix">
  <div class="crumbs">
    <a href="http://www.javaeye.com/forums">论坛首页</a> <span class="arrow">→</span>
    <a href="http://www.javaeye.com/forums/board/Java">Java编程和Java企业应用版</a> <span class="arrow">→</span>
    <a href="http://www.javaeye.com/forums/tag/Spring">Spring</a> <span class="arrow">→</span>
    <h1>Hibernate 与 Spring 多数据源的配置</h1>
  </div>
</div>

<div id="forum_tag_list">
  <a href="http://www.javaeye.com/forums/board/Java">全部</a>
      <a href="http://www.javaeye.com/forums/tag/Hibernate">Hibernate</a>
      <a href="http://www.javaeye.com/forums/tag/Spring">Spring</a>
      <a href="http://www.javaeye.com/forums/tag/Struts">Struts</a>
      <a href="http://www.javaeye.com/forums/tag/iBATIS">iBATIS</a>
      <a href="http://www.javaeye.com/forums/tag/J2EE">企业应用</a>
      <a href="http://www.javaeye.com/forums/tag/Design-Pattern">设计模式</a>
      <a href="http://www.javaeye.com/forums/tag/DAO">DAO</a>
      <a href="http://www.javaeye.com/forums/tag/Object-Domain">领域模型</a>
      <a href="http://www.javaeye.com/forums/tag/OO">OO</a>
      <a href="http://www.javaeye.com/forums/tag/Tomcat">Tomcat</a>
      <a href="http://www.javaeye.com/forums/tag/SOA">SOA</a>
      <a href="http://www.javaeye.com/forums/tag/JBoss">JBoss</a>
      <a href="http://www.javaeye.com/forums/tag/Swing">Swing</a>
  </div>

<div class="actions">
  <div class="buttons"></div>
  <div class="wrapper">
    <div id="forum_advert_f4"></div>
    
    <div class="info">浏览 4738 次</div>
  </div>
</div>



<table id="forum_main" cellspacing="1">
  <thead>
    <tr>
      <th colspan="2" class="topic_title">
        <div class="wrapper"><span class="locked">锁定老贴子</span> <a href="http://www.javaeye.com/topic/78432">主题：Hibernate 与 Spring 多数据源的配置</a></div>
        <div id="topic_rate_box"><a href="#" onclick="rate_topic('good'); return false;">精华帖</a> (0) :: <a href="#" onclick="rate_topic('mid'); return false;">良好帖</a> (0) :: <a href="#" onclick="rate_topic('new'); return false;">新手帖</a> (0) :: <a href="#" onclick="rate_topic('hidden'); return false;">隐藏帖</a> (0)</div>
      </th>
    </tr>
    <tr>
      <td class="first_col">作者</td>
      <td class="last_col">正文</td>
    </tr>
  </thead>
  <tbody id="posts">
    <tr id="281753">
  <td class="postauthor">
    <ul>
      <li class="name">yanxiaohui5522</li>
      <li>等级: 初级会员</li>
      <li><a href="http://yanxiaohui5522.javaeye.com/" target="_blank"><img alt="yanxiaohui5522的博客" class="logo" src="78432_files/user-logo.gif" title="yanxiaohui5522的博客: yanxiaohui5522"></a></li>
      <li>文章: 1</li>
      <li>积分: 35</li>
      <li>来自: ...</li>
      <li><img src="78432_files/offline.gif"></li>
    </ul>
  </td>
  <td class="postcontent">
    <div class="postactions">
      <div class="description">
        发表时间：2007-05-10
        
      </div>
      <div class="links">
        
          
          
            
            <a href="http://app.javaeye.com/links?user_favorite%5Btitle%5D=Hibernate+%E4%B8%8E+Spring+%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90%E7%9A%84%E9%85%8D%E7%BD%AE&amp;user_favorite%5Burl%5D=http%3A%2F%2Fwww.javaeye.com%2Ftopic%2F78432" target="_blank" class="favorite">收藏</a>
          
        
      </div>
    </div>
    <div class="postbody clearfix">
                                            <div style="position: relative;" id="related_topics">相关文章: <span class="close"><a href="#" onclick="$('related_topics').hide();return false;" title="关闭">&nbsp;</a></span>
              <ul>
                                    <li><a href="http://www.javaeye.com/topic/21122" title="动态切换多数据源" target="_blank">动态切换多数据源</a></li>
                                      <li><a href="http://www.javaeye.com/topic/148177" title="请教关于业务服务层实现问题？" target="_blank">请教关于业务服务层实现问题？</a></li>
                                      <li><a href="http://www.javaeye.com/topic/191696" title="JPA + Hibernate 3  CRUD操作历史审计日志的解决方案（附源码） " target="_blank">JPA + Hibernate 3  CRUD操作历史审计日志的解决方案（附源码） </a></li>
                                  </ul>
                <br><span>推荐圈子: <a href="http://jbpm.group.javaeye.com/" target="_blank">JBPM @net</a></span>
                <br><a href="http://www.javaeye.com/wiki/topic/78432" target="_blank">更多相关推荐</a>
              </div>
              <script type="text/javascript">
                new Draggable("related_topics");
              </script>
Spring2.0.1以后的版本已经支持配置多数据源，并且可以在运行的时候动态加载不同的数据源。通过继承
AbstractRoutingDataSource就可以实现多数据源的动态转换。目前做的项目就是需要访问12个数据源，每个数据源的表结构都是相同
的，所以要求数据源的变动对于编码人员来说是透明，也就是说同样SQL语句在不同的环境下操作的数据库是不一样的。具体的配置如下：
<br>一、首先需要写一个静态的键值对照类：
<br>
<br><div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed src="78432_files/clipboard_new.swf" flashvars="clipboard=package%20cn.com.xinli.ccp.dynamicds%3B%0A%0Apublic%20class%20DataSourceMap%20%7B%0A%09public%20static%20final%20String%20Admin%3D%22Admin%22%3B%0A%09public%20static%20final%20String%20Yxh%20%3D%20%22Yxh%22%3B%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15"></div></div><ol class="dp-j" start="1"><li><span><span class="keyword">package</span><span>&nbsp;cn.com.xinli.ccp.dynamicds;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;DataSourceMap&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">final</span><span>&nbsp;String&nbsp;Admin=</span><span class="string">"Admin"</span><span>;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">final</span><span>&nbsp;String&nbsp;Yxh&nbsp;=&nbsp;</span><span class="string">"Yxh"</span><span>;&nbsp;&nbsp;</span></span></li><li><span>}&nbsp;&nbsp;</span></li></ol></div><pre style="display: none;" name="code" class="java">package cn.com.xinli.ccp.dynamicds;

public class DataSourceMap {
	public static final String Admin="Admin";
	public static final String Yxh = "Yxh";
}</pre>
<br>
<br>这个类主要在使用的时候当作获得数据源的标志使用。
<br>二、建立一个获得和设置上下文的类：
<br><div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed src="78432_files/clipboard_new.swf" flashvars="clipboard=package%20cn.com.xinli.ccp.dynamicds%3B%0A%0Apublic%20class%20CustomerContextHolder%20%7B%0A%09private%20static%20final%20ThreadLocal%20contextHolder%20%3D%20%0A%20%20%20%20%20%20%20%20new%20ThreadLocal()%3B%0A%20%20%20%20%0A%09public%20static%20void%20setCustomerType(String%20customerType)%20%7B%0A%09%20%20contextHolder.set(customerType)%3B%0A%09%7D%0A%09%0A%09public%20static%20String%20getCustomerType()%20%7B%0A%09%20%20return%20(String)%20contextHolder.get()%3B%0A%09%7D%0A%09%0A%09public%20static%20void%20clearCustomerType()%20%7B%0A%09%20%20contextHolder.remove()%3B%0A%09%7D%0A%0A%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15"></div></div><ol class="dp-j" start="1"><li><span><span class="keyword">package</span><span>&nbsp;cn.com.xinli.ccp.dynamicds;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;CustomerContextHolder&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">final</span><span>&nbsp;ThreadLocal&nbsp;contextHolder&nbsp;=&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">new</span><span>&nbsp;ThreadLocal();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;setCustomerType(String&nbsp;customerType)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;contextHolder.set(customerType);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;String&nbsp;getCustomerType()&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>&nbsp;(String)&nbsp;contextHolder.get();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;clearCustomerType()&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;contextHolder.remove();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>}&nbsp;&nbsp;</span></li></ol></div><pre style="display: none;" name="code" class="java">package cn.com.xinli.ccp.dynamicds;

public class CustomerContextHolder {
	private static final ThreadLocal contextHolder = 
        new ThreadLocal();
    
	public static void setCustomerType(String customerType) {
	  contextHolder.set(customerType);
	}
	
	public static String getCustomerType() {
	  return (String) contextHolder.get();
	}
	
	public static void clearCustomerType() {
	  contextHolder.remove();
	}


}</pre>
<br>这个主要负责设置上下文环境和获得上下文环境。
<br>三、建立动态数据源类，这个类必须继承AbstractRoutingDataSource：
<br><div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed src="78432_files/clipboard_new.swf" flashvars="clipboard=package%20cn.com.xinli.ccp.dynamicds%3B%0A%0Aimport%20org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource%3B%0A%0Apublic%20class%20DynamicDataSource%20extends%20AbstractRoutingDataSource%20%7B%0A%0A%09protected%20Object%20determineCurrentLookupKey()%20%7B%0A%09%09%2F%2F%20TODO%20Auto-generated%20method%20stub%0A%09%09return%20CustomerContextHolder.getCustomerType()%3B%0A%09%7D%0A%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15"></div></div><ol class="dp-j" start="1"><li><span><span class="keyword">package</span><span>&nbsp;cn.com.xinli.ccp.dynamicds;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="keyword">import</span><span>&nbsp;org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;DynamicDataSource&nbsp;</span><span class="keyword">extends</span><span>&nbsp;AbstractRoutingDataSource&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">protected</span><span>&nbsp;Object&nbsp;determineCurrentLookupKey()&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;TODO&nbsp;Auto-generated&nbsp;method&nbsp;stub</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>&nbsp;CustomerContextHolder.getCustomerType();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>}&nbsp;&nbsp;</span></li></ol></div><pre style="display: none;" name="code" class="java">package cn.com.xinli.ccp.dynamicds;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {

	protected Object determineCurrentLookupKey() {
		// TODO Auto-generated method stub
		return CustomerContextHolder.getCustomerType();
	}

}</pre>
<br>这个类实现了determineCurrentLookupKey方法，该方法返回一个Object，一般是返回字符串，也可以是枚举类型。该方
法中直接使用了CustomerContextHolder.getCustomerType()方法获得上下文环境并直接返回。
<br>四、编写spring的配置文件配置数据源
<br><div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed src="78432_files/clipboard_new.swf" flashvars="clipboard=%3Cbean%20id%3D%22parentDataSource%22%0A%09%09%09class%3D%22org.springframework.jdbc.datasource.DriverManagerDataSource%22%3E%0A%09%09%09%3Cproperty%20name%3D%22driverClassName%22%3E%0A%09%09%09%09%3Cvalue%3ECOM.ibm.db2.jdbc.net.DB2Driver%3C%2Fvalue%3E%0A%09%09%09%3C%2Fproperty%3E%0A%09%09%09%3Cproperty%20name%3D%22url%22%3E%0A%09%09%09%09%3Cvalue%3Ejdbc%3Adb2%3A127.0.0.1%3ATEST%3C%2Fvalue%3E%0A%09%09%09%3C%2Fproperty%3E%0A%09%3C%2Fbean%3E%0A%09%0A%09%3Cbean%20id%3D%22adminDataSource%22%20parent%3D%22parentDataSource%22%3E%0A%09%20%20%20%09%3Cproperty%20name%3D%22username%22%20value%3D%22admin%22%2F%3E%0A%09%20%20%20%09%3Cproperty%20name%3D%22password%22%20value%3D%22master997mb%22%2F%3E%0A%09%3C%2Fbean%3E%0A%09%0A%09%3Cbean%20id%3D%22yxhDataSource%22%20parent%3D%22parentDataSource%22%3E%0A%09%20%20%20%09%3Cproperty%20name%3D%22username%22%20value%3D%22yxh%22%2F%3E%0A%09%20%20%20%09%3Cproperty%20name%3D%22password%22%20value%3D%22yxh%22%2F%3E%0A%09%3C%2Fbean%3E" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15"></div></div><ol class="dp-j" start="1"><li><span><span>&lt;bean&nbsp;id=</span><span class="string">"parentDataSource"</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">class</span><span>=</span><span class="string">"org.springframework.jdbc.datasource.DriverManagerDataSource"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=<span class="string">"driverClassName"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;value&gt;COM.ibm.db2.jdbc.net.DB2Driver&lt;/value&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/property&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=<span class="string">"url"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;value&gt;jdbc:db2:<span class="number">127.0</span><span>.</span><span class="number">0.1</span><span>:TEST&lt;/value&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/property&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/bean&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;bean&nbsp;id=<span class="string">"adminDataSource"</span><span>&nbsp;parent=</span><span class="string">"parentDataSource"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=<span class="string">"username"</span><span>&nbsp;value=</span><span class="string">"admin"</span><span>/&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=<span class="string">"password"</span><span>&nbsp;value=</span><span class="string">"master997mb"</span><span>/&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/bean&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;bean&nbsp;id=<span class="string">"yxhDataSource"</span><span>&nbsp;parent=</span><span class="string">"parentDataSource"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=<span class="string">"username"</span><span>&nbsp;value=</span><span class="string">"yxh"</span><span>/&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=<span class="string">"password"</span><span>&nbsp;value=</span><span class="string">"yxh"</span><span>/&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/bean&gt;&nbsp;&nbsp;</span></li></ol></div><pre style="display: none;" name="code" class="java">&lt;bean id="parentDataSource"
			class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
			&lt;property name="driverClassName"&gt;
				&lt;value&gt;COM.ibm.db2.jdbc.net.DB2Driver&lt;/value&gt;
			&lt;/property&gt;
			&lt;property name="url"&gt;
				&lt;value&gt;jdbc:db2:127.0.0.1:TEST&lt;/value&gt;
			&lt;/property&gt;
	&lt;/bean&gt;
	
	&lt;bean id="adminDataSource" parent="parentDataSource"&gt;
	   	&lt;property name="username" value="admin"/&gt;
	   	&lt;property name="password" value="master997mb"/&gt;
	&lt;/bean&gt;
	
	&lt;bean id="yxhDataSource" parent="parentDataSource"&gt;
	   	&lt;property name="username" value="yxh"/&gt;
	   	&lt;property name="password" value="yxh"/&gt;
	&lt;/bean&gt;</pre>
<br>在这个配置中可以看到首先有个parentDataSource，这个主要配置一些数据源的公用信息，项目中都是链接DB2数据
库；adminDataSource和yxhDataSource是根据不同需要配置的个性化信息，但都必须加parent属性，值为
parentDataSource。这样就配置好了2个数据源信息。当然如果链接的多数据源是不同类型的两个数据库，那么
parentDataSource就可以不要了，直接配置两个不同的数据源链接就可以了。
<br>五、编写spring配置文件配置多数据源映射关系
<br><div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed src="78432_files/clipboard_new.swf" flashvars="clipboard=%3Cbean%20id%3D%22dataSource%22%20class%3D%22cn.com.xinli.ccp.dynamicds.DynamicDataSource%22%3E%0A%09%20%20%20%3Cproperty%20name%3D%22targetDataSources%22%3E%0A%09%20%20%20%20%20%20%3Cmap%20key-type%3D%22java.lang.String%22%3E%0A%09%20%20%20%20%20%20%20%20%20%3Centry%20key%3D%22Yxh%22%20value-ref%3D%22yxhDataSource%22%2F%3E%0A%09%20%20%20%20%20%20%3C%2Fmap%3E%0A%09%20%20%20%3C%2Fproperty%3E%0A%09%20%20%20%3Cproperty%20name%3D%22defaultTargetDataSource%22%20ref%3D%22adminDataSource%22%2F%3E%0A%09%3C%2Fbean%3E" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15"></div></div><ol class="dp-j" start="1"><li><span><span>&lt;bean&nbsp;id=</span><span class="string">"dataSource"</span><span>&nbsp;</span><span class="keyword">class</span><span>=</span><span class="string">"cn.com.xinli.ccp.dynamicds.DynamicDataSource"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=<span class="string">"targetDataSources"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;map&nbsp;key-type=<span class="string">"java.lang.String"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;entry&nbsp;key=<span class="string">"Yxh"</span><span>&nbsp;value-ref=</span><span class="string">"yxhDataSource"</span><span>/&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/map&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/property&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=<span class="string">"defaultTargetDataSource"</span><span>&nbsp;ref=</span><span class="string">"adminDataSource"</span><span>/&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/bean&gt;&nbsp;&nbsp;</span></li></ol></div><pre style="display: none;" name="code" class="java">&lt;bean id="dataSource" class="cn.com.xinli.ccp.dynamicds.DynamicDataSource"&gt;
	   &lt;property name="targetDataSources"&gt;
	      &lt;map key-type="java.lang.String"&gt;
	         &lt;entry key="Yxh" value-ref="yxhDataSource"/&gt;
	      &lt;/map&gt;
	   &lt;/property&gt;
	   &lt;property name="defaultTargetDataSource" ref="adminDataSource"/&gt;
	&lt;/bean&gt;</pre>
<br>在这个配置中第一个property属性配置目标数据源，&lt;map
key-type="java.lang.String"&gt;中的key-type必须要和静态键值对照类DataSourceMap中的值的类型相
同；&lt;entry key="Yxh"
value-ref="yxhDataSource"/&gt;中key的值必须要和静态键值对照类中的值相同，如果有多个值，可以配置多个&lt;
entry&gt;标签。第二个property属性配置默认的数据源。
<br>六、配置hibernate。
<br>Hibernate的配置和普通的hibernate、spring结合的配置一样
<br><div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed src="78432_files/clipboard_new.swf" flashvars="clipboard=%3Cbean%20id%3D%22sessionFactory%22%0A%09%09class%3D%22org.springframework.orm.hibernate3.LocalSessionFactoryBean%22%3E%0A%09%09%3C!--%20to%20override%2C%20use%20the%20%22SpringDatasourceConfig%22%20snippet%20in%20your%20project%20--%3E%0A%09%09%3Cproperty%20name%3D%22dataSource%22%3E%0A%09%09%09%3Cref%20local%3D%22dataSource%22%20%2F%3E%0A%09%09%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22mappingResources%22%3E%0A%09%09%09%3Clist%3E%0A%09%09%09%09%3Cvalue%3E%0A%09%09%09%09%09cn%2Fcom%2Fxinli%2Fccp%2Fentity%2FUser.hbm.xml%0A%09%09%09%09%3C%2Fvalue%3E%0A%09%09%09%09%3Cvalue%3E%0A%09%09%09%09%09cn%2Fcom%2Fxinli%2Fccp%2Fentity%2FTest.hbm.xml%0A%09%09%09%09%3C%2Fvalue%3E%0A%09%09%09%3C%2Flist%3E%0A%09%09%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernateProperties%22%3E%0A%09%09%09%3Cprops%3E%0A%09%09%09%09%3Cprop%20key%3D%22hibernate.dialect%22%3E%0A%09%09%09%09%09org.hibernate.dialect.DB2Dialect%0A%09%09%09%09%3C%2Fprop%3E%0A%09%09%09%09%0A%09%09%09%09%3Cprop%20key%3D%22hibernate.show_sql%22%3Etrue%3C%2Fprop%3E%0A%09%09%09%09%3Cprop%20key%3D%22hibernate.use_outer_join%22%3Etrue%3C%2Fprop%3E%0A%09%09%09%09%3Cprop%20key%3D%22hibernate.jdbc.batch_size%22%3E50%3C%2Fprop%3E%0A%09%09%09%09%3Cprop%20key%3D%22hibernate.jdbc.fetch_size%22%3E5%3C%2Fprop%3E%0A%09%09%09%09%3Cprop%20key%3D%22hibernate.connection.pool_size%22%3E2%3C%2Fprop%3E%0A%09%09%09%09%3Cprop%20key%3D%22hibernate.connection.autocommit%22%3Efalse%3C%2Fprop%3E%0A%09%09%09%09%3Cprop%20key%3D%22hibernate.cache.use_query_cache%22%3Efalse%3C%2Fprop%3E%0A%09%09%09%09%3Cprop%20key%3D%22hibernate.max_fetch_depth%22%3E1%3C%2Fprop%3E%0A%09%09%09%09%3Cprop%20key%3D%22hibernate.bytecode.use_reflection_optimizer%22%3Etrue%3C%2Fprop%3E%0A%09%09%09%3C%2Fprops%3E%0A%09%09%3C%2Fproperty%3E%0A%09%3C%2Fbean%3E%0A%0A%3Cbean%20id%3D%22mydao%22%20class%3D%22cn.com.xinli.ccp.dao.HibernateBaseDao%22%3E%0A%09%09%3Cproperty%20name%3D%22sessionFactory%22%3E%0A%09%09%09%3Cref%20local%3D%22sessionFactory%22%20%2F%3E%0A%09%09%3C%2Fproperty%3E%0A%09%3C%2Fbean%3E" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15"></div></div><ol class="dp-j" start="1"><li><span><span>&lt;bean&nbsp;id=</span><span class="string">"sessionFactory"</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">class</span><span>=</span><span class="string">"org.springframework.orm.hibernate3.LocalSessionFactoryBean"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--&nbsp;to&nbsp;override,&nbsp;use&nbsp;the&nbsp;<span class="string">"SpringDatasourceConfig"</span><span>&nbsp;snippet&nbsp;in&nbsp;your&nbsp;project&nbsp;--&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=<span class="string">"dataSource"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;ref&nbsp;local=<span class="string">"dataSource"</span><span>&nbsp;/&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/property&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=<span class="string">"mappingResources"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;list&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;value&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cn/com/xinli/ccp/entity/User.hbm.xml&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/value&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;value&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cn/com/xinli/ccp/entity/Test.hbm.xml&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/value&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/list&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/property&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=<span class="string">"hibernateProperties"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;props&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop&nbsp;key=<span class="string">"hibernate.dialect"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;org.hibernate.dialect.DB2Dialect&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/prop&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop&nbsp;key=<span class="string">"hibernate.show_sql"</span><span>&gt;</span><span class="keyword">true</span><span>&lt;/prop&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop&nbsp;key=<span class="string">"hibernate.use_outer_join"</span><span>&gt;</span><span class="keyword">true</span><span>&lt;/prop&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop&nbsp;key=<span class="string">"hibernate.jdbc.batch_size"</span><span>&gt;</span><span class="number">50</span><span>&lt;/prop&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop&nbsp;key=<span class="string">"hibernate.jdbc.fetch_size"</span><span>&gt;</span><span class="number">5</span><span>&lt;/prop&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop&nbsp;key=<span class="string">"hibernate.connection.pool_size"</span><span>&gt;</span><span class="number">2</span><span>&lt;/prop&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop&nbsp;key=<span class="string">"hibernate.connection.autocommit"</span><span>&gt;</span><span class="keyword">false</span><span>&lt;/prop&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop&nbsp;key=<span class="string">"hibernate.cache.use_query_cache"</span><span>&gt;</span><span class="keyword">false</span><span>&lt;/prop&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop&nbsp;key=<span class="string">"hibernate.max_fetch_depth"</span><span>&gt;</span><span class="number">1</span><span>&lt;/prop&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop&nbsp;key=<span class="string">"hibernate.bytecode.use_reflection_optimizer"</span><span>&gt;</span><span class="keyword">true</span><span>&lt;/prop&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/props&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/property&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/bean&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&lt;bean&nbsp;id=<span class="string">"mydao"</span><span>&nbsp;</span><span class="keyword">class</span><span>=</span><span class="string">"cn.com.xinli.ccp.dao.HibernateBaseDao"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=<span class="string">"sessionFactory"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;ref&nbsp;local=<span class="string">"sessionFactory"</span><span>&nbsp;/&gt;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/property&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/bean&gt;&nbsp;&nbsp;</span></li></ol></div><pre style="display: none;" name="code" class="java">&lt;bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
		&lt;!-- to override, use the "SpringDatasourceConfig" snippet in your project --&gt;
		&lt;property name="dataSource"&gt;
			&lt;ref local="dataSource" /&gt;
		&lt;/property&gt;
		&lt;property name="mappingResources"&gt;
			&lt;list&gt;
				&lt;value&gt;
					cn/com/xinli/ccp/entity/User.hbm.xml
				&lt;/value&gt;
				&lt;value&gt;
					cn/com/xinli/ccp/entity/Test.hbm.xml
				&lt;/value&gt;
			&lt;/list&gt;
		&lt;/property&gt;
		&lt;property name="hibernateProperties"&gt;
			&lt;props&gt;
				&lt;prop key="hibernate.dialect"&gt;
					org.hibernate.dialect.DB2Dialect
				&lt;/prop&gt;
				
				&lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
				&lt;prop key="hibernate.use_outer_join"&gt;true&lt;/prop&gt;
				&lt;prop key="hibernate.jdbc.batch_size"&gt;50&lt;/prop&gt;
				&lt;prop key="hibernate.jdbc.fetch_size"&gt;5&lt;/prop&gt;
				&lt;prop key="hibernate.connection.pool_size"&gt;2&lt;/prop&gt;
				&lt;prop key="hibernate.connection.autocommit"&gt;false&lt;/prop&gt;
				&lt;prop key="hibernate.cache.use_query_cache"&gt;false&lt;/prop&gt;
				&lt;prop key="hibernate.max_fetch_depth"&gt;1&lt;/prop&gt;
				&lt;prop key="hibernate.bytecode.use_reflection_optimizer"&gt;true&lt;/prop&gt;
			&lt;/props&gt;
		&lt;/property&gt;
	&lt;/bean&gt;

&lt;bean id="mydao" class="cn.com.xinli.ccp.dao.HibernateBaseDao"&gt;
		&lt;property name="sessionFactory"&gt;
			&lt;ref local="sessionFactory" /&gt;
		&lt;/property&gt;
	&lt;/bean&gt;</pre>
<br>关于dao的代码这里就省略了。
<br>七、配置结束，可以使用了。
<br><div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed src="78432_files/clipboard_new.swf" flashvars="clipboard=public%20class%20DaoTest%20extends%20TestCase%20%7B%0A%0A%09public%20void%20testSave()%20throws%20Exception%7B%0A%09%09CustomerContextHolder.setCustomerType(DataSourceMap.Admin)%3B%2F%2F%E8%AE%BE%E7%BD%AE%E6%95%B0%E6%8D%AE%E6%BA%90%0A%09%09%2F%2Fhibernate%E5%88%9B%E5%BB%BA%E5%AE%9E%E4%BD%93%0A%09%09Test%20test%20%3D%20new%20Test()%3B%0A%09%09test.setTest(%2222222222%22)%3B%0A%09%09%0A%09%09mydao.save(test)%3B%2F%2F%E4%BD%BF%E7%94%A8dao%E4%BF%9D%E5%AD%98%E5%AE%9E%E4%BD%93%0A%09%09%0A%09%09CustomerContextHolder.setCustomerType(DataSourceMap.Yxh)%3B%2F%2F%E8%AE%BE%E7%BD%AE%E4%B8%BA%E5%8F%A6%E4%B8%80%E4%B8%AA%E6%95%B0%E6%8D%AE%E6%BA%90%0A%09%09%0A%09%09mydao.save(test)%3B%2F%2F%E4%BD%BF%E7%94%A8dao%E4%BF%9D%E5%AD%98%E5%AE%9E%E4%BD%93%E5%88%B0%E5%8F%A6%E4%B8%80%E4%B8%AA%E5%BA%93%E4%B8%AD%0A%09%09%0A%09%7D%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15"></div></div><ol class="dp-j" start="1"><li><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;DaoTest&nbsp;</span><span class="keyword">extends</span><span>&nbsp;TestCase&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;testSave()&nbsp;</span><span class="keyword">throws</span><span>&nbsp;Exception{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CustomerContextHolder.setCustomerType(DataSourceMap.Admin);<span class="comment">//设置数据源</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//hibernate创建实体</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test&nbsp;test&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Test();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test.setTest(<span class="string">"22222222"</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mydao.save(test);<span class="comment">//使用dao保存实体</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CustomerContextHolder.setCustomerType(DataSourceMap.Yxh);<span class="comment">//设置为另一个数据源</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mydao.save(test);<span class="comment">//使用dao保存实体到另一个库中</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>}&nbsp;&nbsp;</span></li></ol></div><pre style="display: none;" name="code" class="java">public class DaoTest extends TestCase {

	public void testSave() throws Exception{
		CustomerContextHolder.setCustomerType(DataSourceMap.Admin);//设置数据源
		//hibernate创建实体
		Test test = new Test();
		test.setTest("22222222");
		
		mydao.save(test);//使用dao保存实体
		
		CustomerContextHolder.setCustomerType(DataSourceMap.Yxh);//设置为另一个数据源
		
		mydao.save(test);//使用dao保存实体到另一个库中
		
	}
}</pre>在项目中对于编码人员对多数据源的切换可以做成透明的，操作同样的dao，就可以访问不同的数据库了。
      </div>
      
              <div id="topic_copyright">声明：JavaEye文章版权属于作者，受法律保护。没有作者书面许可不得转载。 </div>
        <div id="forum_advert_f5"><span>推荐链接</span><ul><li><a href="http://www.javaeye.com/clicks/170" target="_blank"><span style="color: blue; font-weight: bold;"> 【8月推荐】JAVA技术沙龙活动通知--欢迎并期待您的参与!</span></a></li><li><a href="http://www.javaeye.com/clicks/111" target="_blank"><img src="78432_files/720-60.gif" style="border: 1px solid rgb(255, 255, 255);" alt="taobao，赢在淘宝" title="taobao，赢在淘宝" width="720" height="60">

</a></li></ul></div>
                </td>
  </tr>
  <tr>
    <td class="postauthor"><a href="#">返回顶楼</a></td>
    <td class="postcontent">
      <div class="icons">
        <a href="http://yanxiaohui5522.javaeye.com/" class="icon_www" title="浏览作者的博客">&nbsp;</a>
        <a href="http://yanxiaohui5522.javaeye.com/blog/profile" class="icon_profile" title="浏览作者资料">&nbsp;</a>
        <a href="http://app.javaeye.com/messages/new?message%5Breceiver_name%5D=yanxiaohui5522" class="icon_pm" title="发送站内短信">&nbsp;</a>
        <a href="http://yanxiaohui5522.javaeye.com/blog/guest_book" class="icon_guestbook" title="给作者留言">&nbsp;</a>
        
                
      </div>
                        </td>
  </tr>
  <tr class="sep2"><td colspan="2"></td></tr>
<tr id="303298">
  <td class="postauthor">
    <ul>
      <li class="name">zhunahao</li>
      <li>等级: 初级会员</li>
      <li><a href="http://zhunahao.javaeye.com/" target="_blank"><img alt="zhunahao的博客" class="logo" src="78432_files/8c497a5a-a06f-40bb-9a69-1494bd6170c4.jpg" title="zhunahao的博客: zhunahao"></a></li>
      <li>文章: 6</li>
      <li>积分: 32</li>
      
      <li><img src="78432_files/offline.gif"></li>
    </ul>
  </td>
  <td class="postcontent">
    <div class="postactions">
      <div class="description">
        发表时间：2007-06-04
        
      </div>
      <div class="links">
        
          
          
            
            <a href="http://app.javaeye.com/links?user_favorite%5Btitle%5D=zhunahao%E5%9B%9E%E8%B4%B4%3AHibernate+%E4%B8%8E+Spring+%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90%E7%9A%84%E9%85%8D%E7%BD%AE&amp;user_favorite%5Burl%5D=http%3A%2F%2Fwww.javaeye.com%2Ftopic%2F78432%23303298" target="_blank" class="favorite">收藏</a>
            
            
          
        
      </div>
    </div>
    <div class="postbody clearfix">
      
        这样的方式连接两个数据库,可以使用JDBC进行操作吗?
<br>如何获得Connection?
      </div>
      
                </td>
  </tr>
  <tr>
    <td class="postauthor"><a href="#">返回顶楼</a></td>
    <td class="postcontent">
      <div class="icons">
        <a href="http://zhunahao.javaeye.com/" class="icon_www" title="浏览作者的博客">&nbsp;</a>
        <a href="http://zhunahao.javaeye.com/blog/profile" class="icon_profile" title="浏览作者资料">&nbsp;</a>
        <a href="http://app.javaeye.com/messages/new?message%5Breceiver_name%5D=zhunahao" class="icon_pm" title="发送站内短信">&nbsp;</a>
        <a href="http://zhunahao.javaeye.com/blog/guest_book" class="icon_guestbook" title="给作者留言">&nbsp;</a>
        
                  <a href="#303298" title="zhunahao回帖:Hibernate 与 Spring 多数据源的配置">回帖地址</a>
                
      </div>
                        <div id="post_rate_box_303298" class="post_rate_box"><a href="#" onclick='rate_post(303298, "good"); return false;' class="good" title="好">0</a> <a href="#" onclick='rate_post(303298, "hidden"); return false;' class="hidden" title="差">0</a> </div>                  </td>
  </tr>
  <tr class="sep2"><td colspan="2"></td></tr>
<tr id="307398">
  <td class="postauthor">
    <ul>
      <li class="name">二十一</li>
      <li>等级: 初级会员</li>
      <li><a href="http://caozhenx.javaeye.com/" target="_blank"><img alt="二十一的博客" class="logo" src="78432_files/a257e43b-52cf-389f-916f-850da2d10c97.jpg" title="二十一的博客: 二十一"></a></li>
      <li>文章: 62</li>
      <li>积分: 14</li>
      <li>来自: 深圳</li>
      <li><img src="78432_files/offline.gif"></li>
    </ul>
  </td>
  <td class="postcontent">
    <div class="postactions">
      <div class="description">
        发表时间：2007-06-08
        
      </div>
      <div class="links">
        
          
          
            
            <a href="http://app.javaeye.com/links?user_favorite%5Btitle%5D=%E4%BA%8C%E5%8D%81%E4%B8%80%E5%9B%9E%E8%B4%B4%3AHibernate+%E4%B8%8E+Spring+%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90%E7%9A%84%E9%85%8D%E7%BD%AE&amp;user_favorite%5Burl%5D=http%3A%2F%2Fwww.javaeye.com%2Ftopic%2F78432%23307398" target="_blank" class="favorite">收藏</a>
            
            
          
        
      </div>
    </div>
    <div class="postbody clearfix">
      
        这样做可能无法使用open session in view 吧
      </div>
      
                </td>
  </tr>
  <tr>
    <td class="postauthor"><a href="#">返回顶楼</a></td>
    <td class="postcontent">
      <div class="icons">
        <a href="http://caozhenx.javaeye.com/" class="icon_www" title="浏览作者的博客">&nbsp;</a>
        <a href="http://caozhenx.javaeye.com/blog/profile" class="icon_profile" title="浏览作者资料">&nbsp;</a>
        <a href="http://app.javaeye.com/messages/new?message%5Breceiver_name%5D=%E4%BA%8C%E5%8D%81%E4%B8%80" class="icon_pm" title="发送站内短信">&nbsp;</a>
        <a href="http://caozhenx.javaeye.com/blog/guest_book" class="icon_guestbook" title="给作者留言">&nbsp;</a>
        
                  <a href="#307398" title="二十一回帖:Hibernate 与 Spring 多数据源的配置">回帖地址</a>
                
      </div>
                        <div id="post_rate_box_307398" class="post_rate_box"><a href="#" onclick='rate_post(307398, "good"); return false;' class="good" title="好">0</a> <a href="#" onclick='rate_post(307398, "hidden"); return false;' class="hidden" title="差">0</a> </div>                  </td>
  </tr>
  <tr class="sep2"><td colspan="2"></td></tr>
<tr id="313413">
  <td class="postauthor">
    <ul>
      <li class="name">programmer</li>
      <li>等级: 初级会员</li>
      <li><a href="http://programmer.javaeye.com/" target="_blank"><img alt="programmer的博客" class="logo" src="78432_files/user-logo.gif" title="programmer的博客: programmer"></a></li>
      <li>文章: 47</li>
      <li>积分: 81</li>
      
      <li><img src="78432_files/offline.gif"></li>
    </ul>
  </td>
  <td class="postcontent">
    <div class="postactions">
      <div class="description">
        发表时间：2007-06-15
        
      </div>
      <div class="links">
        
          
          
            
            <a href="http://app.javaeye.com/links?user_favorite%5Btitle%5D=programmer%E5%9B%9E%E8%B4%B4%3AHibernate+%E4%B8%8E+Spring+%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90%E7%9A%84%E9%85%8D%E7%BD%AE&amp;user_favorite%5Burl%5D=http%3A%2F%2Fwww.javaeye.com%2Ftopic%2F78432%23313413" target="_blank" class="favorite">收藏</a>
            
            
          
        
      </div>
    </div>
    <div class="postbody clearfix">
      
        多数据源，怎么支持cache
      </div>
      
                </td>
  </tr>
  <tr>
    <td class="postauthor"><a href="#">返回顶楼</a></td>
    <td class="postcontent">
      <div class="icons">
        <a href="http://programmer.javaeye.com/" class="icon_www" title="浏览作者的博客">&nbsp;</a>
        <a href="http://programmer.javaeye.com/blog/profile" class="icon_profile" title="浏览作者资料">&nbsp;</a>
        <a href="http://app.javaeye.com/messages/new?message%5Breceiver_name%5D=programmer" class="icon_pm" title="发送站内短信">&nbsp;</a>
        <a href="http://programmer.javaeye.com/blog/guest_book" class="icon_guestbook" title="给作者留言">&nbsp;</a>
        
                  <a href="#313413" title="programmer回帖:Hibernate 与 Spring 多数据源的配置">回帖地址</a>
                
      </div>
                        <div id="post_rate_box_313413" class="post_rate_box"><a href="#" onclick='rate_post(313413, "good"); return false;' class="good" title="好">0</a> <a href="#" onclick='rate_post(313413, "hidden"); return false;' class="hidden" title="差">0</a> </div>                  </td>
  </tr>
  <tr class="sep2"><td colspan="2"></td></tr>

  </tbody>
</table>


<div class="actions">
  <div class="buttons"></div>
  <div class="wrapper"></div>
</div>

<div>
  <div class="crumbs">
    <a href="http://www.javaeye.com/forums">论坛首页</a> <span class="arrow">→</span>
    <a href="http://www.javaeye.com/forums/board/Java">Java编程和Java企业应用版</a>
     <span class="arrow">→</span> <a href="http://www.javaeye.com/forums/tag/Spring">Spring</a>
    <br><br>跳转论坛:<select id="forum_urlname" name="forum[urlname]" onchange="window.location='/forums/board/'+this.value"><option value="Java" selected="selected">Java编程和Java企业应用</option>
<option value="web">Web前端技术：AJAX和RIA</option>
<option value="mobile">移动编程和手机应用开发</option>
<option value="cpp">C/C++编程</option>
<option value="Ruby">Ruby编程</option>
<option value="Python">Python编程</option>
<option value="PHP">PHP编程</option>
<option value="Flash">Flash编程</option>
<option value="dotnet">Microsoft .Net</option>
<option value="Tech">综合技术</option>
<option value="develop">软件开发和项目管理</option>
<option value="Industry">行业应用</option>
<option value="New">入门讨论</option>
<option value="Job">招聘求职</option>
<option value="Life">海阔天空</option></select>
    <div id="forum_advert_f6"></div>
  </div>

  
</div>

<div style="display: none;">
  <form action="/posts/rate" id="post_rate_form" method="post" onsubmit="new Ajax.Request('/posts/rate', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;">
    <input id="post_rate_id" name="id" type="hidden">
    <input id="post_rate_type" name="type" type="hidden">
  </form></div>

<script type="text/javascript">
  dp.SyntaxHighlighter.HighlightAll('code', true, true);

  fix_image_size($$('div.postbody img'), 760);

  function rate_post(id, type) {
    Element.update('post_rate_box_' + id, '提交中...');
    new Ajax.Request('/posts/rate', {asynchronous:true, evalScripts:true, parameters:{"id":id, "type":type}});
  }

  function rate_topic(type) {
    Element.update('topic_rate_box', '提交中...');
    new Ajax.Request('/topics/rate', {asynchronous:true, evalScripts:true, parameters:{"id":78432, "type":type}});
  }
</script>


        </div>
        <div id="nav">
  <div class="wrapper">
    <ul>
            <li><a href="http://www.javaeye.com/">首页</a></li>
      <li><a href="http://www.javaeye.com/news">新闻</a></li>
      <li><a href="http://www.javaeye.com/forums" class="selected">论坛</a></li>
      <li><a href="http://www.javaeye.com/ask">问答</a></li>
      <li><a href="http://www.javaeye.com/wiki">知识库</a></li>
      <li><a href="http://www.javaeye.com/blogs">博客</a></li>
      <li><a href="http://www.javaeye.com/groups">圈子</a></li>
      <!--<li><a href="http://www.javaeye.com/hunters">猎头</a></li>//-->
      <li><a href="http://www.javaeye.com/job">招聘</a></li>
      <li><a href="http://www.javaeye.com/index/service">服务</a></li>
      <li class="last"><a href="http://www.javaeye.com/google_search">搜索</a></li>
    </ul>
  </div>
</div>

<div id="channel_nav">
  <ul>
    <li><a href="http://java.javaeye.com/">Java</a></li>
    <li><a href="http://webapp.javaeye.com/">Web</a></li>
    <li><a href="http://ruby.javaeye.com/">Ruby</a></li>
    <li><a href="http://python.javaeye.com/">Python</a></li>
    <li><a href="http://agile.javaeye.com/">敏捷</a></li>
    <li><a href="http://mysql.javaeye.com/">MySQL</a></li>
    <li><a href="http://primeton.javaeye.com/">普元</a></li>
    <li><a href="http://dorado.javaeye.com/">Dorado</a></li>
    <li><a href="http://kingdee.javaeye.com/">金蝶中间件</a></li>
    <li class="last"><a href="http://book.javaeye.com/">图书</a></li>
  </ul>
</div>
      </div>
      <div id="footer">
  <div id="site_nav">
    <ul>
      <li><a href="http://www.javaeye.com/index/service">广告服务</a></li>
      <li><a href="http://webmaster.javaeye.com/">JavaEye黑板报</a></li>
      <li><a href="http://www.javaeye.com/index/aboutus">关于我们</a></li>
      <li><a href="http://www.javaeye.com/index/contactus">联系我们</a></li>
      <li class="last"><a href="http://www.javaeye.com/index/friend_links">友情链接</a></li>
    </ul>
  </div>
  <div id="copyright">
    © 2003-2009 JavaEye.com.   All rights reserved. 上海炯耐计算机软件有限公司 [ 沪ICP备05023328号 ] 
  </div>
</div>

    </div>
    
  <div id="chat_input" style="display: none;">
    <iframe id="chat_iframe" marginwidth="0" marginheight="0" allowtransparency="true" frameborder="0" scrolling="no"></iframe>
  </div>
  <div id="chat_button"><a href="#" style="text-decoration: none; color: black;" onclick="$('chat_input').show();$('chat_iframe').src='/chat/widget';return false;">说点啥吧 <img alt="Chat" src="78432_files/chat.gif"></a></div>

<script type="text/javascript">
  document.write("<img src='http://stat.javaeye.com/?url="+ encodeURIComponent(document.location.href) + "&referrer=" + encodeURIComponent(document.referrer) + "&logged_in=yes" + "' width='0' height='0' />");
</script><img src="78432_files/a.gif" width="0" height="0">
  </body></html>